package org.andy.wxpay.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;



public class WebUtil {

	public static Object getSessionAttribute(HttpServletRequest req, String key) {
		Object ret = null;
		try {
			ret = req.getSession(false).getAttribute(key);
		} catch (Exception e) {
		}
		return ret;
	}
	
	public static void response(HttpServletResponse response, String result) {
		
		try {
			
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String packJsonp(String callback, String json) {
		if (json == null) {
			json = "";
		}
		if (callback == null || callback.isEmpty()) {
			return json;
		}
		return json;
	}
	
	 public static String  bihua(String cnStr) {
		 String bihua="";
	    	for (int i = 0; i < cnStr.length(); i++) {
	    		char  item =  cnStr.charAt(i);
	    		String str=String.valueOf(item);
		 try {
				
				URL u = new URL("http://route.showapi.com/1524-5?showapi_appid=81190&hanzi="+str+"&showapi_sign=d9d9142f38984506bda62747c8a9fbc1");
		    	InputStream in = u.openStream();
		    	ByteArrayOutputStream out = new ByteArrayOutputStream();
		    	try {
		    	byte buf[] = new byte[1024];
		    	int read = 0;
		    	while ((read = in .read(buf)) > 0) {
		    	out.write(buf, 0, read);
		    	}
		    	} finally {
		    	if ( in != null) {
		    	in .close();
		    	}
		    	}
		    	byte sc[] = out.toByteArray();
		    	String sr=new String(sc, "utf-8");
		    	JSONObject jsStr = JSON.parseObject(sr);
		    	JSONObject jo=jsStr.getJSONObject("showapi_res_body");
		    	 bihua =bihua+jo.getString("bihua")+",";
		    	
			} catch (Exception e) {
			e.printStackTrace();
			}

		 
		 
			}
	    	return bihua;
		}

}
